//
//  SNSleepChartVc.m
//  project
//
//  Created by 黄建华 on 2017/12/9.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNSleepChartVc.h"
#import "SNSleepChartView.h"
#define LEFTWIDH 40.0
@interface SNSleepChartVc ()

@property (nonatomic, strong) SNSleepChartView *ChartView;
@property (nonatomic, strong) NSMutableArray   *modelArray;
@property (nonatomic, assign) SelectDateType   lastType;


@end

@implementation SNSleepChartVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _ChartView                 = [[SNSleepChartView alloc] initWithFrame:SNFRAME(LEFTWIDH, 70 +FitSimpleIpx(0, 20), HHHWIDTH -LEFTWIDH, HHHHEIGHT *0.4 -FitSimpleIpx(0, 20))];
    _ChartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_ChartView];
    
    NSArray *array1 = [NSArray arrayWithObjects:SN_Text(@"大于良好天数"), SN_Text(@"有效睡眠总时长"), SN_Text(@"日均有效睡眠时长"), SN_Text(@"单次最长睡眠"), SN_Text(@"有效睡眠占比"), SN_Text(@"睡眠质量"), nil];
    
    CGFloat height      = FitSimpleSize(75, 80, 100, 120, 130);
    CGFloat labelHeight = FitSimpleSize(0, 0, 10, 20, 27);
    UIView  *line0      = [[UIView alloc] init];
    line0.frame           = SNFRAME(0, _ChartView.origin.y + HHHHEIGHT *0.43, HHHWIDTH, 5.0);
    line0.backgroundColor = SNCOLORGRAYLINE;
    [self.view addSubview:line0];
    
    UIView *line1 = [[UIView alloc] init];
    line1.frame           = SNFRAME(0, _ChartView.origin.y + HHHHEIGHT *0.43 + height, HHHWIDTH, 1);
    line1.backgroundColor = SNCOLORGRAYLINE;
    [self.view addSubview:line1];
    
    UIView *line2 = [[UIView alloc] init];
    line2.frame           = SNFRAME(0, _ChartView.origin.y + HHHHEIGHT *0.43 + height*2, HHHWIDTH, 5.0);
    line2.backgroundColor = SNCOLORGRAYLINE;
    [self.view addSubview:line2];
    
    UIView *line3 = [[UIView alloc] init];
    line3.frame           = SNFRAME(HHHWIDTH /3, _ChartView.origin.y + HHHHEIGHT *0.43, 1, height*2);
    line3.backgroundColor = SNCOLORGRAYLINE;
    [self.view addSubview:line3];
    
    UIView *line4 = [[UIView alloc] init];
    line4.frame           = SNFRAME(HHHWIDTH /3 *2, _ChartView.origin.y + HHHHEIGHT *0.43, 1, height*2);
    line4.backgroundColor = SNCOLORGRAYLINE;
    [self.view addSubview:line4];
    
    for (int i = 0; i < 6; i++) {
        UILabel *unitLabel = [[UILabel alloc] init];
        unitLabel.textAlignment = NSTextAlignmentCenter;
        unitLabel.text          = array1[i];
        unitLabel.textColor     = SNCOLORGRAY;
        unitLabel.font          = SNSFONT(12.0);
        if (i < 3) {
            unitLabel.frame = SNFRAME(HHHWIDTH /3 *i, labelHeight +_ChartView.origin.y + HHHHEIGHT *0.43 + 40, HHHWIDTH /3, 20);
        } else {
            unitLabel.frame = SNFRAME(HHHWIDTH /3 *i - HHHWIDTH, labelHeight +_ChartView.origin.y + HHHHEIGHT *0.43 + 40 + height, HHHWIDTH /3, 20);
        }
        [self.view addSubview:unitLabel];
    }
    
    //数据加载
    NSArray *array2 = [NSArray arrayWithObjects:@"4 天", @"6h 14 min", @"8h 14 min", @"1h 08 min", @"60%", @"良好", nil];
    // 数值显示Label
    for (int i = 0; i < 6; i++) {
        UILabel *unitLabel = [[UILabel alloc] init];
        unitLabel.textAlignment = NSTextAlignmentCenter;
        //        unitLabel.text = array2[i];
        unitLabel.textColor = SNCOLORBLACK;
        unitLabel.font      = SNFONT(14.0);
        unitLabel.tag       = 100 + i;
        if (i < 3) {
            unitLabel.frame = SNFRAME(HHHWIDTH /3 *i, labelHeight +_ChartView.origin.y + HHHHEIGHT *0.43 + 40 - 22, HHHWIDTH /3, 20);
        } else {
            unitLabel.frame = SNFRAME(HHHWIDTH /3 *i - HHHWIDTH, labelHeight +_ChartView.origin.y + HHHHEIGHT *0.43 + 40 + height - 22, HHHWIDTH /3, 20);
        }
        [self.view addSubview:unitLabel];
        
        if (i > 0 && i < 4) {
            //         [unitLabel setAttributedText:[SN_PublicClass label1With:unitLabel.text withFontsize:10.0 withChangeText1:SN_Text(@"h") withChangeText2:SN_Text(@"min")]];
        }
    }
    
    [self selectType:SelectDateWeek];
}

- (void)selectType:(SelectDateType)type {
    _lastType = type;
    //    NSLog(@"周月年选择了>>>>>>%d",type);
    NSMutableArray *array = [NSMutableArray array];
    
    if (type == SelectDateWeek) {
        for (int i = 0; i < 7; i++) {
            NSDate   *first       = [SN_PublicClass getMonthBeginAndEndWith:SN_PublicClass.selectDate withType:1];
            NSDate   *Date        = [first dateAfterDay:i];
            NSString *firstString = [Date dateToDayString];
            NSString *sss         = [NSString stringWithFormat:@"SELECT compareEff FROM SleepModel WHERE dateString ='%@' and dateString <='%@' and user_id = '%@'", firstString, [[NSDate date]dateToDayString], SN_USER.user_id];
            NSString *sleepValue  = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:sss arguments:nil];
            
            int sleepS = sleepValue.floatValue *100;
            [array addObject:StrByInt(sleepS)];
        }
    }
    if (type == SelectDateMonth) {
        NSInteger monthCount = SN_PublicClass.selectDate.daysInMonth;
        for (int i = 0; i < monthCount; i++) {
            NSDate   *first       = [SN_PublicClass getMonthBeginAndEndWith:SN_PublicClass.selectDate withType:2];
            NSDate   *Date        = [first dateAfterDay:i];
            NSString *firstString = [Date dateToDayString];
            NSString *sss         = [NSString stringWithFormat:@"SELECT compareEff FROM SleepModel WHERE dateString ='%@' and dateString <='%@' and user_id = '%@'", firstString, [[NSDate date]dateToDayString], SN_USER.user_id];
            NSString *sleepValue  = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:sss arguments:nil];
            int      sleepS       = sleepValue.floatValue *100;
            [array addObject:StrByInt(sleepS)];
        }
    }
    
    if (type == SelectDateYear) {

        for (int i = 1; i <= 12; i++) {
            
            NSString *beginDateString = [NSString stringWithFormat:@"%d-%02d-01",  SN_PublicClass.selectDate.year,i];
            NSString *endDateString = [NSString stringWithFormat:@"%d-%02d-31",  SN_PublicClass.selectDate.year,i];
            NSString *monthStep  = [NSString stringWithFormat:@"SELECT AVG(compareEff) AS OrderTotal FROM SleepModel WHERE  durations >0 and dateString >='%@' and  dateString <='%@' and user_id = '%@'",beginDateString, endDateString, SN_USER.user_id];
            NSString *sleepValue = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:monthStep arguments:nil];
            int      sleepS      = sleepValue.floatValue *100;
            [array addObject:StrByInt(sleepS)];
        }
    }
    
    [_ChartView UpdateViewWithType:type withArray:array];
    [self updateValueLabel:type];
}

- (void)updateValueLabel:(SelectDateType)type {
    //数据加载
    NSMutableArray *array2 = [NSMutableArray array];
    // 周达标天数
    
    NSArray *time = [SN_PublicClass getBeginDateStringAndEndWith:SN_PublicClass.selectDate withType:type];
    
    NSString *finish  = [NSString stringWithFormat:@"SELECT SUM(sleepquality) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and compareEff >=0.6 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *finishs = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:finish arguments:nil];
    
    [array2 addObject:[NSString stringWithFormat:@"%d %@", finishs.intValue, SN_Text(@"天")]];
    
    NSString *sleep  = [NSString stringWithFormat:@"SELECT SUM(effDuration) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *sleeps = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:sleep arguments:nil];
    [array2 addObject:[NSString stringWithFormat:@"%d%@ %02d%@", sleeps.intValue/60, SN_Text(@"h"), sleeps.intValue%60, SN_Text(@"min")]];
    
    NSString *avesleep  = [NSString stringWithFormat:@"SELECT AVG(effDuration) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and durations >0 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *avesleeps = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:avesleep arguments:nil];
    [array2 addObject:[NSString stringWithFormat:@"%d%@ %02d%@", avesleeps.intValue/60, SN_Text(@"h"), avesleeps.intValue%60, SN_Text(@"min")]];
    
    NSString *maxsleep  = [NSString stringWithFormat:@"SELECT MAX(durations) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and durations >0 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *maxsleeps = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:maxsleep arguments:nil];
    [array2 addObject:[NSString stringWithFormat:@"%d%@ %02d%@", maxsleeps.intValue/60, SN_Text(@"h"), maxsleeps.intValue%60, SN_Text(@"min")]];
    
    NSString *avecompareEff  = [NSString stringWithFormat:@"SELECT AVG(compareEff) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and durations >0 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *avecompareEffs = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:avecompareEff arguments:nil];
    int      compareEffs     = avecompareEffs.floatValue *100;
    [array2 addObject:[NSString stringWithFormat:@"%d %%", compareEffs]];
    
    NSString *avequality = @"";
    avequality = [SleepModel WithQuality:avecompareEffs.floatValue];
    
    [array2 addObject:avequality];
    
    if (array2.count > 0) {
        for (UILabel *label in self.view.subviews) {
            if ([label isKindOfClass:[UILabel class]]) {
                if (label.tag >= 100) {
                    label.text = array2[label.tag - 100];
                }
            }
        }
    }
}

- (void)updateView:(KKBLEAcceptType)type {
    if (type == KKBLEAcceptTypeDataSleep) {
        [self selectType:_lastType];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

